﻿using System;
using UnityConfiguration.Infrastructure;
using mpu = Microsoft.Practices.Unity;

namespace UnityConfiguration
{
    /// <summary>
    /// parameter instance going to be Dependency injected by the container.
    /// </summary>
    public class Dependency : InjectionParameterValue, IValue
    {
        private string _Name;
        private string _TypeName;
        /// <summary>
        /// Name of the dependency to resolve.
        /// </summary>
        public string Name {
            get { return _Name; }
            set { _Name = value; }
        }

        /// <summary>
        /// Type of the dependency to resolve.
        /// </summary>
        public string TypeName {
            get { return _TypeName; }
            set { _TypeName = value; }
        }

        /// <summary>
        /// Return an instance of <see cref="InjectionParameterValue"/> based
        /// on the contents of this 
        /// </summary>
        /// <param name="targetType">Type of the containing parameter.</param>
        /// <returns>The created InjectionParameterValue, ready to pass to the container config API.</returns>
        public override mpu.InjectionParameterValue CreateParameterValue(Type targetType) {
            Type dependencyType;
            if (string.IsNullOrEmpty(TypeName)) {
                dependencyType = targetType;
            } else {
                dependencyType = TypeResolver.ResolveType(TypeName);
            }
            return new mpu.ResolvedParameter(dependencyType, Name);
        }
    }
}
